package com.eternal.base.data.source;

import androidx.paging.DataSource;

import com.eternal.base.concat.DeviceModelInfo;
import com.eternal.base.concat.NotificationName;
import com.eternal.base.database.BaseDatabase;
import com.eternal.base.database.dao.DeviceDao;
import com.eternal.base.database.dao.LogDao;
import com.eternal.base.database.dao.NotificationDao;
import com.eternal.base.database.entity.Log;
import com.eternal.base.database.entity.Notification;

import java.util.Calendar;
import java.util.List;

public class LogSource implements ILogSource {
    private LogDao lDao;
    private NotificationDao nDao;
    private DeviceDao dDao;

    public LogSource() {
        BaseDatabase database = BaseDatabase.getInstance();
        lDao = database.logDao();
        nDao = database.notificationDao();
        dDao = database.deviceDao();
    }



    @Override
    public void addLog(Log log) {
        lDao.insertLog(log);
    }

    @Override
    public DataSource.Factory<Integer, Log> getLogs(String mac, byte port,List<Byte> types, List<Byte> models) {
        if (models == null || models.size() == 0) {
            return lDao.loadByType(mac, port, types);
        }
        return lDao.loadByTypeAndModel(mac, port, types, models);
    }

    @Override
    public DataSource.Factory<Integer, Log> getLogs(String mac, List<Byte> types) {
        return lDao.loadCByType(mac, types);
    }

    @Override
    public Log getFirstLog(String mac) {
        Log log = lDao.loadFirstLog(mac);
        if (log == null) {
            log = new Log();
            Calendar calendar = Calendar.getInstance();
            calendar.set(2000, 1, 1);
            log.time = calendar.getTimeInMillis();
            log.id = 0;
        }
        return log;
    }

    @Override
    public Log getFirstELog(String mac, byte port) {
        Log log = lDao.loadFirstELog(mac,port);
        if (log == null) {
            log = new Log();
            Calendar calendar = Calendar.getInstance();
            calendar.set(2000, 1, 1);
            log.time = calendar.getTimeInMillis();
            log.id = 0;
        }
        return log;
    }

    @Override
    public Log getFirstCLog(String mac) {
        Log log = lDao.loadFirstCLog(mac);
        if (log == null) {
            log = new Log();
            log.mac = mac;
            log.time = 0;
            log.id = -1;
        }
        return log;
    }

    @Override
    public long getLogTime(String mac, byte port) {
        return dDao.loadLogTime(mac,port);
    }

    @Override
    public void setLogTime(String mac,byte port, long time) {
        if (dDao.loadLogTime(mac,port) != time) {
            dDao.updateDeviceLogTime(mac,port, time);
        }
    }

    @Override
    public int getNewLogNum(String mac, byte port) {
        return lDao.numByMacAndTime(mac, port, dDao.loadLogTime(mac,port));
    }

    @Override
    public Notification getNotification(String mac, byte port,int id, byte type) {
        return nDao.loadByMacAndId(mac, port, id, type);
    }

    @Override
    public DeviceModelInfo getModelInfo(String mac) {
        return dDao.loadModelInfo(mac);
    }

    @Override
    public boolean isDegree(String mac) {
        return dDao.isDegree(mac);
    }

    @Override
    public List<NotificationName> getNotificationNames(String mac,byte port) {
        return nDao.loadMacAllName(mac,port);
    }

    @Override
    public long getDeviceConnectTime(String mac){
        return dDao.getConnectTime(mac);
    }
}
